package com.szqhyxj.youxiangju.biz.core;

import android.support.annotation.NonNull;

import com.szqhyxj.youxiangju.sys.BaseResult;
import com.szqhyxj.youxiangju.sys.RetrofitFacade;

import java.io.File;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;

public class ImageUploader{

    public interface ImageService {
        @Multipart
        @POST("file/upload")
        Observable<BaseResult<ImageUploadResult>> upload(@Part MultipartBody.Part file, @Part("type") RequestBody type, @Part("token") RequestBody token);

        @Multipart
        @POST("file/upload")
        Observable<BaseResult<ImageUploadResult>> uploadCard(@Part MultipartBody.Part file, @Part("type") RequestBody type, @Part("cardNo") RequestBody cardNo, @Part("token") RequestBody token);
    }

    public static Observable<BaseResult<ImageUploadResult>> uploadIdcardFace(File file) {
        MultipartBody.Part filePart = getMultiPart(file);
        String ID_CARD_FACE = "idcardFace";

        return RetrofitFacade.Companion
                .getUploadRetrofit()
                .create(ImageService.class)
                .upload(
                        filePart,
                        RequestBody.create(MediaType.parse("text/plain"), ID_CARD_FACE),
                        getToken()
                );
    }

    public static Observable<BaseResult<ImageUploadResult>> uploadIdcardBack(File file) {
        MultipartBody.Part filePart = getMultiPart(file);
        String ID_CARD_BACK = "idcardBack";
        return RetrofitFacade.Companion
                .getUploadRetrofit()
                .create(ImageService.class)
                .upload(
                        filePart,
                        RequestBody.create(MediaType.parse("text/plain"), ID_CARD_BACK),
                        getToken()
                );
    }

    public static Observable<BaseResult<ImageUploadResult>> uploadDeposit(File file, String cardNo) {
        MultipartBody.Part filePart = getMultiPart(file);
        String DEPOSIT = "deposit";
        return RetrofitFacade.Companion
                .getUploadRetrofit()
                .create(ImageService.class)
                .uploadCard(
                        filePart,
                        RequestBody.create(MediaType.parse("text/plain"), DEPOSIT),
                        RequestBody.create(MediaType.parse("text/plain"), cardNo),
                        getToken()
                );
    }

    public static Observable<BaseResult<ImageUploadResult>> uploadCredit(File file, String cardNo) {
        MultipartBody.Part filePart = getMultiPart(file);
        String CREDIT = "credit";
        return RetrofitFacade.Companion
                .getUploadRetrofit()
                .create(ImageService.class)
                .uploadCard(
                        filePart,
                        RequestBody.create(MediaType.parse("text/plain"), CREDIT),
                        RequestBody.create(MediaType.parse("text/plain"), cardNo),
                        getToken()
                );
    }

    public static Observable<BaseResult<ImageUploadResult>> uploadHandleIDCard(File file) {
        MultipartBody.Part filePart = getMultiPart(file);
        String IDCARD_HANDLER = "idCardHandler";
        return RetrofitFacade.Companion
                .getUploadRetrofit()
                .create(ImageService.class)
                .upload(
                        filePart,
                        RequestBody.create(MediaType.parse("text/plain"), IDCARD_HANDLER),
                        getToken()
                );
    }

    public static Observable<BaseResult<ImageUploadResult>> uploadSign(File file) {
        MultipartBody.Part filePart = getMultiPart(file);
        String APP_SIGN = "appSign";
        return RetrofitFacade.Companion
                .getUploadRetrofit()
                .create(ImageService.class)
                .upload(
                        filePart,
                        RequestBody.create(MediaType.parse("text/plain"), APP_SIGN),
                        getToken()
                );
    }


    public static Observable<BaseResult<ImageUploadResult>> uploadqucikSign(File file) {
        MultipartBody.Part filePart = getMultiPart(file);
        String APP_SIGN = "qucikSign";
        return RetrofitFacade.Companion
                .getUploadRetrofit()
                .create(ImageService.class)
                .upload(
                        filePart,
                        RequestBody.create(MediaType.parse("text/plain"), APP_SIGN),
                        getToken()
                );
    }


    @NonNull
    private static RequestBody getToken() {
        return RequestBody.create(MediaType.parse("text/plain"), TokenManager.INSTANCE.getMToken());
    }

    @NonNull
    private static MultipartBody.Part getMultiPart(File file) {
        return MultipartBody.Part.createFormData(
                "file",
                file.getName(),
                RequestBody.create(MediaType.parse("image/*"), file)
        );
    }



}